package com.jereh.topnews.widget;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jereh.topnews.R;

/**
 * Created by Administrator on 2016/11/22.
 */
public class IToast extends Toast {

    /**
     * Construct an empty Toast object.  You must call {@link #setView} before you
     * can call {@link #show}.
     *
     * @param context The context to use.  Usually your {@link Application}
     *                or {@link Activity} object.
     */
    public IToast(Context context) {
        super(context);
    }
    public static Toast makeText(Context context,String text,IGravity gravity,int... params){
        Toast result = new Toast(context);

        LayoutInflater inflate = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout v = (LinearLayout)inflate.inflate(R.layout.itoast_view, null);
        TextView tv = (TextView)v.findViewById(R.id.message);
        tv.setText(text);
        result.setView(v);
        if(gravity!=null) {
            result.setGravity(gravity.getGravity(), gravity.getX(), gravity.getY());
        }
        result.setDuration(params[0]);

        if(params.length>1) {
            ((ImageView)v.findViewById(R.id.topImg)).setImageResource(params[1]);
        } else {
            v.findViewById(R.id.topImg).setVisibility(View.GONE);
        }
        if(params.length>2) {
            v.setBackgroundResource(params[2]);
        }
        return result;
    }
}
